<?php namespace Summer\Hjd\Models;

use Model;
use RainLab\User\Models\User as UserModel;

/**
 * Model
 */
class DetailedBonusModel extends Model
{
    use \Winter\Storm\Database\Traits\Validation;
    

    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_hjd_detailed_bonus';

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
    public $hasOne = [
        'change' => ['Summer\Hjd\Models\ChangeModel', 'key' => 'slug','otherKey'=>'change_slug'],
        'target' => ['RainLab\User\Models\User', 'key' => 'id','otherKey'=>'user_id'],
        'relate' => ['RainLab\User\Models\User', 'key' => 'id','otherKey'=>'relate_user_id']
    ];
    /**
     * 用户搜索
     * @param $query
     * @param $field
     * @return bool
     */
    public function scopeTargetUser($query,$field)
    {
        $user=UserModel::where('userphone',$field)->orWhere('username',$field)->first();
        if(!$user){
            return false;
        }else{
            return $query->where('user_id',$user->id);
        }
    }
}
